//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    recordTime: 0,
    interval: 0,
    tempFilePath: "xx",
    loadingHide: false,  //false显示加载中
    availAmount: "0.00" //余额
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function () {
    var that = this;

    this.setData({
      winHeight: app.data.winHeight
    });

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res);
        app.getUserId(res.code,function(){
          that.setData({
            loadingHide: app.data.loadingHide
          });
          that.getAccount();
        });
        
      }
    });

    

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          console.log(res.userInfo);
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          //存储用户信息
          app.storeUserInfo();
        }
      })
    }


  },
  getUserInfo: function(e) {
    
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  //创建红包
  createBonus: function(){
    var that = this;
    that.setData({
      loadingHide: false
    });

    wx.getSetting({
      success: function (res) {

        //处理权限的问题
        if (!res.authSetting['scope.userInfo']) {
          that.setData({
            loadingHide: true
          });
          console.log("用户信息权限不足");
          app.toSetAuth();
          return;
        } else {
          //保存一次用户信息
          //执行用户信息获取，并且回传后台保存。
          wx.getUserInfo({
            success: res => {
                app.globalData.userInfo = res.userInfo
                //存储用户信息
                app.storeUserInfo();

                var dataForm = {
                  "userId": app.data.userid,
                  "actSecretKey": that.data.word,
                  "totalFee": that.data.amount * 100,
                  "joinPeople": that.data.count
                };
                if (!dataForm.actSecretKey) {
                  wx.showModal({
                    title: '提示',
                    content: "口令不能为空",
                    success: function (res) {
                      if (res.confirm) {
                        //用户点击确认

                      } else if (res.cancel) {

                      }
                    }
                  });
                  that.setData({
                    loadingHide: true
                  });
                  return;
                }
                if (!dataForm.totalFee) {
                  wx.showModal({
                    title: '提示',
                    content: "赏金不能为空",
                    success: function (res) {
                      if (res.confirm) {
                        //用户点击确认

                      } else if (res.cancel) {

                      }
                    }
                  });
                  that.setData({
                    loadingHide: true
                  });
                  return;
                }
                if (!dataForm.joinPeople) {
                  wx.showModal({
                    title: '提示',
                    content: "数量不能为空",
                    success: function (res) {
                      if (res.confirm) {
                        //用户点击确认

                      } else if (res.cancel) {

                      }
                    }
                  });
                  that.setData({
                    loadingHide: true
                  });
                  return;
                }
                console.log(dataForm);
                var url = app.data.url + "createBonusActivity.do";
                app.sendPost(url, dataForm, function (res) {
                  that.setData({
                    loadingHide: true
                  });
                  console.log(res);

                  //判断业务逻辑
                  if (res) {
                    if (res.result == "100") {

                    } else {
                      wx.showModal({
                        title: '提示',
                        content: res.message,
                        success: function (res) {
                          if (res.confirm) {
                            //用户点击确认

                          } else if (res.cancel) {

                          }
                        }
                      });
                    }
                  }


                  //返回支付需要的参数，并且还返回activityId
                  var data = res.data;

                  if (data && data.prePayId) {
                    wx.requestPayment({
                      'timeStamp': data.timeStamp,
                      'nonceStr': data.nonceStr,
                      'package': data.prePayId,
                      'signType': data.signType,
                      'paySign': data.paySign,
                      'success': function (res) {
                        console.log(res);
                        wx.showToast({
                          title: "成功!",
                          icon: 'succes',
                          duration: 1000,
                          mask: true
                        });
                        console.log(data.activityId);
                        //跳转到对应的红包详情
                        wx.navigateTo({
                          url: '/pages/bonus_detail/bonus_detail?activityid=' + data.activityId,
                        })
                      },
                      'fail': function (res) {
                        console.log("失败");
                        console.log(res);
                        var errMsg = "支付失败!"
                        if (res && res.err_desc) {
                          errMsg = res.err_desc;
                        }
                        wx.showToast({
                          title: errMsg,
                          icon: 'fail',
                          duration: 1000,
                          mask: true
                        })
                      },
                      'complete': function (res) {
                        console.log("complete");
                        console.log(res);
                      }
                    })
                  } else if (data && data.activityId) {
                    console.log(data.activityId);
                    //跳转到对应的红包详情
                    wx.navigateTo({
                      url: '/pages/bonus_detail/bonus_detail?activityid=' + data.activityId,
                    })
                  }

                });





              }
          });
        }
      }
    });




    
  },
  toBonusList: function(){

    wx.navigateTo({
      url: '/pages/bonus_list/bonus_list',
    })
  },
  //口令
  wordInput: function(e){
    this.setData({
      word: e.detail.value
    });
  },
  //金额
  amountInput: function (e) {
    this.setData({
      amount: e.detail.value
    });
  },
  //数量
  countInput: function (e) {
    this.setData({
      count: e.detail.value
    });
  },
  //获取账户金额
  getAccount: function () {
    var that = this;
    var url = app.data.url + "/account.do";
    var dataForm = {
      'userId': app.data.userid
    };
    app.sendPost(url, dataForm, function (res) {
      console.log(res);
      if(res.result == "100"){
        that.setData({
          availAmount: res.data.availAmount
        });
      }else{
        app.showMessage("登录失败,请关闭再试");
      }
    });
  },
  onPullDownRefresh: function () {
    this.getAccount();
    //隐藏下拉框
    wx.stopPullDownRefresh();

  },
  //去提现
  towithdraw: function(){
    wx.navigateTo({
      url: '/pages/withdraw/withdraw',
    })
  }

})
